local guanli = fk.CreateSkill{
  name = "xiaobai__guanli"
}

Fk:loadTranslationTable{
  ["xiaobai__guanli"] = "观历",
  [":xiaobai__guanli"] = "回合开始时，你可以将牌堆底的至多五张牌以任意顺序置于牌堆顶；"..
    "手牌数大于你的其他角色的回合结束时，你可以观看牌堆顶的五张牌并将其中一张置于牌堆底。",

  ["#xiaobai__guanli"] = "观历：你可以将牌堆底的至多五张牌以任意顺序置于牌堆顶",
}

local guanli_active = fk.CreateSkill{
  name = "xiaobai__guanli_active",
}

guanli_active:addEffect("active", {
  interaction = function (self, player)
    return UI.Spin{
      from = 1,
      to = 5,
      default = 5
    }
  end,
  card_filter = Util.FalseFunc,
  target_filter = Util.FalseFunc,
})

guanli:addEffect(fk.TurnStart, {
  anim_type = "control",
  can_trigger = function (self, event, target, player, data)
    return target == player and player:hasSkill(guanli.name)
  end,
  on_cost = function (self, event, target, player, data)
    local done, ret = player.room:askToUseActiveSkill(player, {
      skill_name = "xiaobai__guanli_active",
      prompt = "#xiaobai__guanli"
    })
    if done and ret then
      event:setCostData(self, {num = ret.interaction})
      return true
    end
  end,
  on_use = function (self, event, target, player, data)
    local num = event:getCostData(self).num
    local room = player.room
    local cards = table.reverse(room:getNCards(num, "bottom"))
    
    local visible_data = {} 
    for _, cid in ipairs(cards) do
      visible_data[tostring(cid)] = false
    end
    for _, cid in ipairs(player:getTableMark("xiaobai__guanli_known")) do
      visible_data[tostring(cid)] = true
    end
    room:moveCardTo(cards, Card.Processing, nil, fk.ReasonJustMove, guanli.name, nil, false, player)
    
    local ret = room:askToCustomDialog(player, {
      skill_name = guanli.name,
      qml_path = "packages/xiaobai/qml/ArrangeUnknownCardBox.qml",
      extra_data = {
        cards = {{}, cards}, 
        prompt = guanli.name,
        size = 0, 
        capacities = {num, num},
        limits = {0, 0},
        free_arrange = true,
        names = {"Top", "Bottom"},
        pattern = ".", 
        poxi_type = "xiaobai__guanli", 
        cancelable = false,
        visible_data = visible_data
      } 
    })
    if ret then
      local cards = table.reverse(ret[1])
      room:moveCardTo(cards, Card.DrawPile, nil, fk.ReasonPut, guanli.name, nil, false, player)
    else
      room:moveCardTo(cards, Card.DrawPile, nil, fk.ReasonPut, guanli.name, nil, false, player)
    end
    
    
    room:cleanProcessingArea(cards, guanli.name)
  end
})

guanli:addEffect(fk.TurnEnd, {
  can_trigger = function (self, event, target, player, data)
    return target ~= player and player:hasSkill(guanli.name) and target:getHandcardNum() > player:getHandcardNum()
  end,
  on_use = function (self, event, target, player, data)
    local room = player.room
    local cards = table.slice(room.draw_pile, 1, 6)
    local ret = room:askToArrangeCards(player, {
      skill_name = guanli.name,
      card_map = {cards, "Top", "Bottom"},
      box_size = 0,
      default_choice = {{}, {cards[1]}},
      free_arrange = false,
      max_limit = {5, 1},
      min_limit = {1, 1}, 
    })
    room:moveCards{
      ids = ret[2],
      drawPilePosition = -1,
      moveReason = fk.ReasonPut,
      proposer = player,
      toArea = Card.DrawPile,
      skillName = guanli.name,
    }
    room:addTableMark(player, "xiaobai__guanli_known", ret[2][1])
  end
})

guanli:addEffect(fk.AfterCardsMove, {
  can_refresh = function (self, event, target, player, data)
    return true
  end,
  on_refresh = function (self, event, target, player, data)
    for _, move in ipairs(data) do
      for _, info in ipairs(move.moveInfo) do
        player.room:removeTableMark(player, "xiaobai__guanli_known", info.cardId)
      end
    end
  end
})

guanli:addEffect(fk.AfterDrawPileShuffle, {
  can_refresh = function (self, event, target, player, data)
    return true
  end,
  on_refresh = function (self, event, target, player, data)
    player.room:setPlayerMark(player, "xiaobai__guanli_known", 0)
  end
})

Fk:addPoxiMethod{
  name = "xiaobai__guanli",
  card_filter = function (to_select, selected, data, extra_data)
    return true
  end,
  feasible = function (selected, data, extra_data)
    local all_card_nums = 0
    for _, cds in ipairs(extra_data) do
      all_card_nums = all_card_nums + #cds
    end
    return #data[1] == all_card_nums
  end
}

local skels = {}
table.insert(skels, guanli)
table.insert(skels, guanli_active)

return skels